#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/* 
 * Copyright © 2012, MIMOS, All Rights Reserved 
 * 
 * WebUtil.java
 * Modification History: 
 **********************************************************
 * Date       		Author            			Comments
 * Mar 23, 2012		magesh.george	        	Modified
 * Mar 27, 2012		Venkaiah Chowdary Koneru	Added list prop helper method
 * 											and removed unnecessary methods
 **********************************************************
 */
package ${package}.commons.support;

import ${package}.dto.ListProp;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * An utility class.
 * 
 * @author magesh.george
 */
public final class WebUtil
{
    /**
     * Constructor. Private to prevent unnecessary instantiation.
     */
    private WebUtil()
    {
    }

    /**
     * initiate a new ListProp object
     * 
     * @param page
     *            page number
     * @param pageSize
     *            number of records per page. can be null/zero
     * @param defaultPageSize
     *            default page setting from properties file. only used when
     *            pageSize is null/zero
     * @param sortProperty
     *            sort property
     * @param sortOrder
     *            sorting order
     * @return ListProp object
     */
    public static ListProp toJQueryListProp(final Integer page,
            final Integer pageSize, final Integer defaultPageSize,
            final String sortProperty, final String sortOrder)
    {
        Integer localPageSize = pageSize;
        Integer localPage = page;
        if (localPageSize == null)
        {
            localPageSize = defaultPageSize;
        }

        if (localPage == null)
        {
            if (localPageSize.intValue() < 0)
            {
                localPage = Integer.valueOf("-1");
            }
            else
            {
                localPage = Integer.valueOf("1");
            }
        }

        ListProp listProp = new ListProp();

        // Set pagination Start and End index values
        setStartAndEndIndex(listProp, localPage, localPageSize);

        // sort property
        if (StringUtils.hasText(sortProperty))
        {
            // sort order
            boolean sortAscending = (StringUtils.hasText(sortOrder) && sortOrder
                    .equalsIgnoreCase("desc")) ? false : true;

            listProp.setSortProperty(sortProperty);
            listProp.setSortAscending(sortAscending);
        }

        return listProp;
    }

    /**
     * Determines to set or not to set page, start and end index values for
     * pagination.If the passed in value for <code>page</code> is
     * <code>-1</code>, then index values (page, startIndex and endIndex) will
     * not be set.
     * 
     * @param listProp
     *            List Prop object
     * @param page
     *            page number. -1 for all.
     */
    private static void setStartAndEndIndex(final ListProp listProp,
            Integer page, Integer localPageSize)
    {
        if (isRequestedForAllRecords(page, localPageSize))
        {
            listProp.setPage(Long.parseLong("1"));
            return;
        }

        if (!isPageValueNegative(page))
        {
            listProp.setPage(page.longValue());
        }
        else
        {
            listProp.setPage(Long.parseLong("1"));
        }

        // Set Start and end index values
        long startIndexLong = (localPageSize * listProp.getPage())
                - localPageSize;
        long endIndexLong = startIndexLong + localPageSize - 1;

        listProp.setStartIndex(startIndexLong);
        listProp.setEndIndex(endIndexLong);
    }

    /**
     * evaluates whether the request is for all records or normal request.
     * 
     * @param page
     * @param pageSize
     * @return true | false
     */
    private static boolean isRequestedForAllRecords(Integer page,
            Integer pageSize)
    {
        if (isPageSizeValueNegative(pageSize) || isPageValueNegative(page))
        {
            return true;
        }

        return false;
    }

    /**
     * checks whether the page value is negative or not.
     * 
     * @param page
     * @return true | false
     */
    private static boolean isPageValueNegative(Integer page)
    {
        Assert.notNull(page);

        if (page.intValue() > 0)
        {
            return false;
        }

        return true;
    }

    /**
     * checks whether the pageSize value is negative or not.
     * 
     * @param pageSize
     * @return true | false
     */
    private static boolean isPageSizeValueNegative(Integer pageSize)
    {
        Assert.notNull(pageSize);

        if (pageSize.intValue() > 0)
        {
            return false;
        }

        return true;
    }
}